package com.ruoyi.storeUser.service.impl;

import java.util.List;

import com.ruoyi.common.core.domain.entity.StoreUser;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.http.HttpUtils;
import com.ruoyi.system.service.ISysConfigService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.storeUser.mapper.StoreUserMapper;
import com.ruoyi.storeUser.service.IStoreUserService;

/**
 * 门店用户Service业务层处理
 *
 * @author ruoyi
 * @date 2021-11-17
 */
@Service
public class StoreUserServiceImpl implements IStoreUserService
{
    @Autowired
    private StoreUserMapper storeUserMapper;
    @Autowired
    private ISysConfigService sysConfigService;


    /**
     * 查询门店用户
     *
     * @param id 门店用户主键
     * @return 门店用户
     */
    @Override
    public StoreUser selectStoreUserById(Long id)
    {
        return storeUserMapper.selectStoreUserById(id);
    }

    @Override
    public StoreUser selectStoreUserByPhone(StoreUser storeUser)
    {
        return storeUserMapper.selectStoreUserByPhone(storeUser);
    }


    /**
     * 查询门店用户列表
     *
     * @param storeUser 门店用户
     * @return 门店用户
     */
    @Override
    public List<StoreUser> selectStoreUserList(StoreUser storeUser)
    {
        return storeUserMapper.selectStoreUserList(storeUser);
    }

    /**
     * 新增门店用户
     *
     * @param storeUser 门店用户
     * @return 结果
     */
    @Override
    public int insertStoreUser(StoreUser storeUser)
    {
        storeUser.setCreateTime(DateUtils.getNowDate());
        return storeUserMapper.insertStoreUser(storeUser);
    }

    @Override
    public String converWechatOpenId(String code) {
        String appId = sysConfigService.selectConfigByKey("app_id_store");
        String appSecret = sysConfigService.selectConfigByKey("app_secret_store");
        String url = "https://api.weixin.qq.com/sns/jscode2session?" +
                "appid=" + appId + "&" + "secret=" + appSecret + "&" +
                "js_code=" + code + "&" +
                "grant_type=authorization_code";
        String content = HttpUtils.doGet(url, "UTF-8", 100, 1000);
        System.out.println("B端用户"+content);
        return content;
    }

    @Override
    public StoreUser selectStoreUserByOpenId(String openId) {
        return storeUserMapper.selectStoreUserByOpenId(openId);
    }

    @Override
    public StoreUser selectStoreUserByOpenIdOrPhone(String openid, String purePhoneNumber) {
        return storeUserMapper.selectStoreUserByOpenIdOrPhone(openid,purePhoneNumber);
    }

    /**
     * 修改门店用户
     *
     * @param storeUser 门店用户
     * @return 结果
     */
    @Override
    public int updateStoreUser(StoreUser storeUser)
    {
        storeUser.setUpdateTime(DateUtils.getNowDate());
        return storeUserMapper.updateStoreUser(storeUser);
    }

    /**
     * 批量删除门店用户
     *
     * @param ids 需要删除的门店用户主键
     * @return 结果
     */
    @Override
    public int deleteStoreUserByIds(Long[] ids)
    {
        return storeUserMapper.deleteStoreUserByIds(ids);
    }


    /**
     * 批量逻辑删除门店用户
     *
     * @param ids 需要删除的门店用户主键
     * @return 结果
     */
    @Override
    public int logicDeleteStoreUser(Long[] ids,String delType,Long delUser)
    {
        return storeUserMapper.logicDeleteStoreUser(ids,delType,delUser);
    }

    /**
     * 删除门店用户信息
     *
     * @param id 门店用户主键
     * @return 结果
     */
    @Override
    public int deleteStoreUserById(Long id)
    {
        return storeUserMapper.deleteStoreUserById(id);
    }
}
